package com.shanzmoo.db.sys.entity.vo;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统用户权限信息
 * @author: Gzhao 2020/12/14
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ApiModel("系统用户权限信息")
public class UserPermissionVo {

    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("所属门店id")
    private Integer outletId;

    @ApiModelProperty("所属门店名称")
    private String outletName;

    @ApiModelProperty("用户权限")
    private List<String> permissionList;

    public static UserPermissionVo create(UserPermissionDto dto) {
        String permissionStr = dto.getPermissionStr();
        if (StrUtil.isNotBlank(dto.getPermissionStr()) && permissionStr.startsWith(",")){
            permissionStr = permissionStr.substring(1, permissionStr.length());
        }

        List<String> permissionList = StrUtil.isNotBlank(permissionStr) ? Arrays.asList(permissionStr.split(",")) : null;
        return UserPermissionVo.builder()
                .userId(dto.getUserId())
                .username(dto.getUsername())
                .outletId(dto.getOutletId())
                .outletName(dto.getOutletName())
                .permissionList(permissionList)
                .build();
    }
}
